﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ZK = ZooKeeperNet;

namespace WorkStation.Frameworks.EasyBus.Repositories.ZooKeeper
{
    /// <summary>
    /// ZooKeeper扩展
    /// </summary>
    public static class ZooKeeperExtensions
    {
        /// <summary>
        /// 等待ZK完全连接
        /// </summary>
        /// <param name="zoo"></param>
        public static void WaitToConnectionCompleted(this ZK.ZooKeeper zoo, TimeSpan timeout) 
        {
            DateTime begin = DateTime.Now;
            
            while (zoo.State != ZK.ZooKeeper.States.CONNECTED) 
            {
                if (DateTime.Now - begin > timeout) 
                {
                    throw new EasyBusException("ZooKeeper连接超时");
                }

                System.Threading.Thread.Sleep(100);
            }
        }
    }
}
